﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace WAD.DAL
{
    /// <summary>
    /// 文章数据库操作类
    /// </summary>
    public class DaoArticle : DAO
    {
        /// <summary>
        /// 添加一篇文章
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static bool Add(MArticle m)
        {
            String sql = "insert into article(title,key_word,logo,content,[user_id],is_recommend)values(@title,@key_word,@logo,@content,@user_id,@is_recommend)";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@title", DbType.String, m.title);
            db.AddInParameter(cmd, "@key_word", DbType.String, m.key_word);
            db.AddInParameter(cmd, "@logo", DbType.String, m.logo);
            db.AddInParameter(cmd, "@content", DbType.String, m.content);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@is_recommend", DbType.Boolean, m.is_recommend);
            return db.ExecuteNonQuery(cmd) > 0;
        }
        /// <summary>
        /// 添加并返回新的ID
        /// </summary>
        /// <param name="m"></param>
        /// <param name="isReturn"></param>
        /// <returns></returns>
        public static int Add(MArticle m, bool isReturn)
        {
            String sql = "insert into article(title,key_word,logo,content,[user_id],is_recommend)values(@title,@key_word,@logo,@content,@user_id,@is_recommend);select @@IDENTITY";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@title", DbType.String, m.title);
            db.AddInParameter(cmd, "@key_word", DbType.String, m.key_word);
            db.AddInParameter(cmd, "@logo", DbType.String, m.logo);
            db.AddInParameter(cmd, "@content", DbType.String, m.content);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@is_recommend", DbType.Boolean, m.is_recommend);
            object o = db.ExecuteScalar(cmd);
            if (o == null) return 0;
            return Convert.ToInt32(o);
        }

        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static MArticle GetEntity(int id)
        {
            String sql = "select id,title,key_word,logo,content,channel_id from article where state=1 and id = " + id;
            object obj = BindEntity(sql, new MArticle());
            return obj == null ? null : (MArticle)obj;
        }

        /// <summary>
        /// 获取一个实体对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Dictionary<string, object> GetDetail(int id)
        {
            String sql = "select id,title,key_word,content,logo,user_id,CONVERT(varchar(20), [date], 20) [date],click,is_recommend from article where state=1 and id = " + id;
            return BindMap(sql);
        }

        /// <summary>
        /// 删除一篇文章
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public static bool Delete(int id, int userId)
        {
            return ExecuteNonQuery(string.Format("delete article where id = {0} and user_id ={1}", id, userId)) > 0;
        }

        /// <summary>
        /// 隐藏一篇文章
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public static bool Hide(int id, int userId)
        {
            return ExecuteNonQuery(string.Format("update article set [state] = 0 where id ={0} and user_id ={1}", id, userId)) > 0;
        }

        /// <summary>
        /// 文章点击量+1
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static bool UpdateClick(int id, int clickNumber = 1)
        {
            return ExecuteNonQuery(string.Format("update article set click=(click+{0}) where id ={1}", clickNumber, id)) > 0;
        }

        /// <summary>
        /// 更新一篇文章
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static bool Update(MArticle m)
        {
            String sql = "update article set title = @title,key_word = @key_word,logo = @logo,content = @content,is_recommend = @is_recommend where [user_id] = @user_id and id = @id";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@title", DbType.String, m.title);
            db.AddInParameter(cmd, "@key_word", DbType.String, m.key_word);
            db.AddInParameter(cmd, "@logo", DbType.String, m.logo);
            db.AddInParameter(cmd, "@content", DbType.String, m.content);
            db.AddInParameter(cmd, "@is_recommend", DbType.Boolean, m.is_recommend);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@id", DbType.Int32, m.id);
            return db.ExecuteNonQuery(cmd) > 0;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页显示的总条数</param>
        /// <returns></returns>
        public static PageData GetList(int pageIndex, int pageSize, MArticle m, string fields = "")
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[state] = 1");
            if (m.is_recommend != null)
            {
                //按频道id过滤
                sb.Append("  and is_recommend = ").Append(m.is_recommend == true ? 1 : 0);
            }

            if (m.channel_id != 0)
            {
                //按频道id过滤
                sb.Append(" and channel_id = ").Append(m.channel_id);
            }

            if (m.user_id != 0)
            {
                sb.Append(" and [user_id] = ").Append(m.user_id);
            }


            if (!String.IsNullOrEmpty(m.key_word))
            {
                //sb.Append(" and (").Append(String.Format("key_word like '%{0}%' OR content like '%{0}%')", QD.WX.Utill.Util.FilterSql(m.key_word)));
                sb.Append(" and (").Append(String.Format("key_word like '%{0}%')", QD.WX.Utill.Util.FilterSql(m.key_word)));
            }

            if (string.IsNullOrEmpty(fields))
                return GetPageList("id,title,logo,click,CONVERT(varchar(20), [date], 20) [date]", "id desc", pageIndex, pageSize, sb.ToString(), "article");
            else
                return GetPageList("id,title,logo,click,CONVERT(varchar(20), [date], 20) [date]," + fields, "id desc", pageIndex, pageSize, sb.ToString(), "article");
        }


        public static int GetCount(int user_id)
        {
            object o = ExecuteScalar(string.Format("select count(1) from fc_article where state=1 and user_id={0}", user_id));
            if (o == null || o == DBNull.Value)
                return 0;
            return Convert.ToInt32(o);
        }
    }
}
